package com.smxy.mall.config;

import com.smxy.mall.model.Audience;
import com.smxy.mall.model.Current;
import com.smxy.mall.utils.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.AuditorAware;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Optional;

@Component
public class AuditorConfig implements AuditorAware<String> {

    @Autowired
    private Audience audience;

    @Override
    public Optional<String> getCurrentAuditor() {
        // 这里应根据实际业务情况获取具体信息
        ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String token = sra.getRequest().getHeader("token");
        String auth = "";
        if(StringUtils.isEmpty(token)){
            auth = "未有token";
        }else{
            Current current = JwtTokenUtil.getCurrentUser(token,audience.getBase64Secret());
            auth = current.getUserId()+"-"+current.getUserName();
        }
        return Optional.of(auth);
    }
}

